<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    <input type="text" id="index">
    <input type="text" id="userName">
    <input type="text" id="passWord">
    <input type="button" value="添加">

    <table border="1" width=700 align="center">
        <thead>
            <tr>
                <th>序号</th>
                <th>用户名</th>
                <th>密码</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</body>
<script>
    var oum = document.getElementById("userName");
    var opw = document.querySelector("#passWord");
    var oindex = document.querySelectorAll("#index")[0];
    var obtn = document.getElementsByTagName("input")[3];
    var otbody = document.querySelector("table tbody");

    // 1.绑定事件
    obtn.onclick = function(){
        // 2. 创建td，设置内容
        var td1 = document.createElement("td");
        td1.innerHTML = oindex.value;
        var td2 = document.createElement("td");
        td2.innerHTML = oum.value;
        var td3 = document.createElement("td");
        td3.innerHTML = opw.value;
        var td4 = document.createElement("td");
        td4.innerHTML = "删除";

        // 3. 创建tr，将td插入到tr内部
        var tr = document.createElement("tr");
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        
        // 4. 将tr插入到tbody内部
        otbody.appendChild(tr);

        // 5. 给当前td4绑定事件，点击时删除当前tr
        td4.onclick = function(){
            tr.remove();
        }
    }


</script>
</html>